package com.jdy.haoduoai.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.jdy.haoduoai.util.Tools;

/**
 * 
 * @author Hufei
 * 
 */
public class ExpandableLayout extends LinearLayout {
  @SuppressWarnings("unused")
  private String TAG = ExpandableLayout.class.getSimpleName();

  private int layoutHeight = -1;
  private int OPEN_LAYOUT_TIMES = 20;
  private int SLEEP_TIME = 15;
  private float layoutStep = 8;

  private OnExpandFinishListener onExpand = null;
  private OnCollapseFinishListener onCollapse = null;

  public interface OnExpandFinishListener {
    public void onExpandFinish();
  }

  public interface OnCollapseFinishListener {
    public void onCollapseFinish();
  }

  public void setOnExpandFinishListener(OnExpandFinishListener oeListener) {
    onExpand = oeListener;
  }

  public void setOnCollapseFinishListener(OnCollapseFinishListener ocListener) {
    onCollapse = ocListener;
  }

  public void setLayoutHeight(int height) {
    layoutHeight = height;
    layoutStep = Tools.getPixelRatio(this.getContext()) * 1.0f * layoutHeight
        / OPEN_LAYOUT_TIMES;
  }

  public ExpandableLayout(Context context) {
    super(context);
  }

  public ExpandableLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void collapse() {
    new Thread() {
      @Override
      public void run() {
        for (int i = 0; i < OPEN_LAYOUT_TIMES + 1; i++) {
          final int ii = i;
          post(new Thread() {
            @Override
            public void run() {
              LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                  (int) ((OPEN_LAYOUT_TIMES - ii) * layoutStep));
              setLayoutParams(params);
              if (ii == OPEN_LAYOUT_TIMES) {
                setVisibility(View.GONE);
                if (null != onCollapse) {
                  onCollapse.onCollapseFinish();
                }
              }
            }
          });

          try {
            Thread.sleep(SLEEP_TIME);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }.start();
  }

  public void expand() {
    if (layoutHeight <= 0) {
      return;
    }

    LayoutParams params = new LayoutParams(
        LayoutParams.MATCH_PARENT, 0);
    setLayoutParams(params);
    setVisibility(View.VISIBLE);

    new Thread() {
      @Override
      public void run() {
        for (int i = 0; i < OPEN_LAYOUT_TIMES + 1; i++) {
          final int ii = i;
          post(new Thread() {
            @Override
            public void run() {
              LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                  (int) (ii * layoutStep));
              setLayoutParams(params);

              if (ii == OPEN_LAYOUT_TIMES) {
                if (null != onExpand) {
                  onExpand.onExpandFinish();
                }
              }
            }
          });

          try {
            Thread.sleep(SLEEP_TIME);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }.start();
  }
}
